Registered S3 method overwritten by 'tsibble':
method from
as_tibble.grouped_df dplyr
Attaching package: 'tsibble'
The following object is masked from 'package:lubridate':
interval
The following objects are masked from 'package:base':
intersect, setdiff, union
# Example: Cache la Poudre River at Mouth (USGS site 06752260)poudre_flow <-readNWISdv(siteNumber ="06752260", # Download data from USGS for site 06752260parameterCd ="00060", # Parameter code 00060 = discharge in cfs)startDate ="2013-01-01", # Set the start dateendDate ="2023-12-31") |># Set the end daterenameNWISColumns() |># Rename columns to standard names (e.g., "Flow", "Date")mutate(Date =yearmonth(Date)) |># Convert daily Date values into a year-month format (e.g., "2023 Jan")group_by(Date) |># Group the data by the new monthly Datesummarise(Flow =mean(Flow))
# Convert to tsibble (monthly data, with Date as index)poudre_ts <- poudre_flow |>as_tsibble(index = Date)
2. Plotting the time series
library(ggplot2)library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
# Basic ggplot of flow over timep <-ggplot(poudre_ts, aes(x = Date, y = Flow)) +geom_line(color ="blue") +labs(title ="Monthly Average Streamflow - Cache la Poudre River",y ="Flow (cfs)", x ="Date") +theme_minimal()# Animate with plotlyggplotly(p)
3. Subseries
library(feasts)
Loading required package: fabletools
Attaching package: 'fabletools'
The following object is masked from 'package:yardstick':
accuracy
The following object is masked from 'package:parsnip':
null_model
The following objects are masked from 'package:infer':
generate, hypothesize
# Subseries plot to show seasonal patternspoudre_ts |>gg_subseries(Flow)
4. Decompose
library(fabletools)# STL decomposition with a seasonal window (say 13 months for annual seasonality)decomp <- poudre_ts |>model(STL(Flow ~season(window ="periodic"))) |>components()# Plot the componentsautoplot(decomp)